package com.lhx.product.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.lhx.product.domain.dto.ProductCategoryDTO;
import com.lhx.product.domain.dto.ProductCategoryWithChildrenItem;
import com.lhx.product.domain.po.ProductCategoryPO;
import com.lhx.product.domain.vo.ProductCategoryVO;
import com.lhx.product.mapper.ProductCategoryMapper;
import com.lhx.product.service.ProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：李洪祥
 * @Package：com.lhx.product.service.impl
 * @Project：lhx-RuoYi
 * @name：ProductCategoryServiceImpl
 * @Date：2024/5/28 下午11:27
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 查询商品分类列表
     * @return
     */
    @Override
    public List<ProductCategoryVO> findList(ProductCategoryDTO productCategoryDTO) {
        return productCategoryMapper.findList(productCategoryDTO);
    }

    /**
     * 添加商品分类
     * @param productCategoryPO
     * @return
     */
    @Override
    public int create(ProductCategoryPO productCategoryPO) {
        ProductCategoryPO productCategory = new ProductCategoryPO();
        productCategory.setProductCount(0);
        BeanUtils.copyProperties(productCategoryPO, productCategory);
        //没有父分类时为一级分类
        setCategoryLevel(productCategory);
        int count = productCategoryMapper.insertSelective(productCategory);
        //创建筛选属性关联
        List<Long> productAttributeIdList = productCategoryPO.getProductAttributeIdList();
//        if(!CollectionUtils.isEmpty(productAttributeIdList)){
//            insertRelationList(productCategory.getId(), productAttributeIdList);
//        }
        return count;
    }


//    private void insertRelationList(Long productCategoryId, List<Long> productAttributeIdList) {
//        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
//        for (Long productAttrId : productAttributeIdList) {
//            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
//            relation.setProductAttributeId(productAttrId);
//            relation.setProductCategoryId(productCategoryId);
//            relationList.add(relation);
//        }
//        productCategoryAttributeRelationDao.insertList(relationList);
//    }





    /**
     * 修改商品分类
     * @param id
     * @param productCategoryPO
     * @return
     */
    @Override
    public int update(Long id, ProductCategoryPO productCategoryPO) {
        return productCategoryMapper.update(id, productCategoryPO);
    }

    /**
     * 根据id查询商品分类
     * @param id
     * @return
     */
    @Override
    public ProductCategoryVO getItem(Long id) {
        return productCategoryMapper.getItem(id);
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {
        return productCategoryMapper.delete(id);
    }

    /**
     * 修改导航栏显示状态
     * @param ids
     * @param navStatus
     * @return
     */
    @Override
    public int updateNavStatus(List<Long> ids, Integer navStatus) {
        return productCategoryMapper.updateNavStatus(ids, navStatus);
    }

    /**
     *
     * @param ids
     * @param showStatus
     * @return
     */
    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        return productCategoryMapper.updateShowStatus(ids, showStatus);
    }

    /**
     * 查询所有一级分类及子分类
     * @return
     */
    @Override
    public List<ProductCategoryWithChildrenItem> listWithChildren() {
        return productCategoryMapper.listWithChildren();
    }

    /**
     * 设置分类级别
     * @param productCategoryPO
     */
    private void setCategoryLevel(ProductCategoryPO productCategoryPO) {
        //没有父分类时为一级分类
        if (productCategoryPO.getParentId() == 0) {
            productCategoryPO.setLevel(0);
        } else {
            //有父分类时选择根据父分类level设置
            ProductCategoryPO parentCategory = productCategoryMapper.selectByPrimaryKey(productCategoryPO.getParentId());
            if (parentCategory != null) {
                parentCategory.setLevel(parentCategory.getLevel() + 1);
            } else {
                parentCategory.setLevel(0);
            }
        }
    }





}
